import axios from './config.js'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

// 导出所有的 api接口
export default {
    login(user) {
        return axios.post('/api/token', user, { loading: true, permission: false })
            .then(res => {
                // 登录成功后延迟跳转，确保 loading 能显示出来
                setTimeout(() => {
                    // 这里可以写登录成功后的跳转逻辑，例如：
                    // this.$router.push({ name: 'home' });
                    // 或 window.location.reload();
                }, 300); // 延迟300ms
                return res;
            });
    },

    register(data) {
        const formData = {
            username: data.username,
            password: data.password,
            email: data.email,
            name: data.username,  
            tel: data.tel || '',  
            // 角色为整数类型
            role: data.role || 1, 
            clazz: data.clazz || ''  
        };
        
        return axios.post('/api/register', formData, {
            permission: false,  // 不需要token验证
            loading: true,     
            headers: {
                'Content-Type': 'application/json'
            }
        });
    },

    getAccessToken(refresh) {
        // 刷新令牌 换取令牌
        return axios.post("/api/token/refresh", {refresh}, {
            headers:{
                "Content-Type": "application/x-www-form-urlencoded",
            },
            permission:false
            
        })
    },
    search(params) {
        // 搜索文章
        return axios.post("/task/search/", params, {
                headers:{
                "Content-Type": "application/json",
            },
            permission: true
        })
    },
    register(registerData){
        // 注册用户
        return axios.post("/user/register", registerData, {
            headers:{
                "Content-Type": "application/json",
            },
            permission:false
        })
    },
    getClazzList() {
        return axios.get('/user/clazz', {
            headers:{
                "Content-Type": "application/json",
            },
            permission:false
        })
        
    },
    modifyPassword(passwordData) {
        // 修改密码
        return axios.put('/user/password_reset', passwordData, {
            headers:{
                "Content-Type": "application/json",
            },
            permission: true
        })
    },
    getTaskList(params){
        // 获取任务列表
        return axios.post("/task/show/", params,{
                headers:{
                "Content-Type": "application/json",
            },
            permission: true
        })
    },
    getLanguage(user_id){
        // 获取任务语言信息
        return axios.post("task/language_detail/", {
            user_id: user_id
        }, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    setCodeAndState(params){
        // 上传代码内容和会不会做状态
        return axios.post("task/set_code/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    teacherSearch(params){
        return axios.post("/teacher/teacher_search/", params, {
            headers:{
                "Content-Type": "application/json",
            },
            permission: true
        })
    },
    updatestate(taskId){
        // 更新任务状态
        // 确保发送正确的参数格式，使用对象包装taskId和status
        const params = {
            task_id: taskId,
            status: 4  // 设置为已检查状态
        };
        return axios.post("task/updatestatus/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    update_state(params){
        // 更新开始和结束状态
        return axios.post("task/update_state/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getTaskExtraList(){
        // 获取学生标记为"不会做"的任务列表
        return axios.post("/task/explain/", {}, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getTeacherList(params){
        // 查询教师列表
        return axios.post("teacher/teacherQuery/",params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getGroupList(params){
        // 获取小组成员
        return axios.post("teacher/studentQuery/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    assignTask(params){
        // 分配任务
        return axios.post("teacher/assignTask/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    deleteTask(params){
        // 穿梭框删除任务
        return axios.post("teacher/deleteTask/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getAssignedUsers(params){
        // 获取已分配任务的用户
        return axios.post("teacher/assignedUsers/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getAnswerAnduser(params){
        // 获取用户的答案和用户信息
        return axios.post("teacher/answerAndUser/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    get_condition_list(){
        // 获取语言和模块列表
        return axios.get("teacher/get_condition_list/", {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    checkStudentTask(params){
        // 教师检查学生任务
        return axios.post("teacher/checkStudentTask/", params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getTaskStatus(params = {}){
        // 获取所有用户的任务状态统计数据
        return axios.get('/task/get_status/', {
            params: params,
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    reset(user_id){
        return axios.get('/user/resetPassword',{
            params: {
                user_id: user_id
            },
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    disableUser(user_id){
        return axios.get('/user/disableUser',{
            params: {
                user_id: user_id
            },
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    assign_Task(params){
        return axios.post('/task/assignTask/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    get_group(){
        // 获取小组列表
        return axios.get('/group/get_group/', {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    getAssignedGroup(params){
        // 获取已分配到本组的用户
        return axios.post('/group/get_assigned_group/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    assignGroupUser(params){
        // 分配小组成员
        return axios.post('/group/assignGroupUser/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    deleteGroupUser(params){
        // 删除小组成员
        return axios.post('/group/deleteGroupUser/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    updateGroupName(params){
        // 更新小组名称
        return axios.post('/group/updateGroupName/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    deleteGroup(params){
        // 删除小组
        return axios.post('/group/deleteGroup/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    addGroup(params){
        // 添加小组
        return axios.post('/group/addGroup/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    assignTasksToGroup(params){
        // 分配任务到小组
        return axios.post('/group/assignTasksToGroup/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    },
    selectAllUserTask(params){
        // 获取小组成员的任务信息
        return axios.post('/group/selectAllUserTask/', params, {
            headers: {
                "Content-Type": "application/json"
            },
            permission: true
        })
    }
}
